﻿@model EquipManagement.Models.Equipment

@{
    ViewBag.Title = "创建设备";
}

@Styles.Render("~/Content/bootstrap-datepicker")

<h2>创建设备</h2>


<form id="form" action="@Url.Action("Create")" method="post" enctype="multipart/form-data">
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>设备</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger", @id = "__RequestVerificationToken" })
        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <label for="TypeId" class="control-label col-md-2">设备类型</label>
            <div class="col-md-10">
                @Html.DropDownList("TypeId", null, htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.TypeId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.PurchaseDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PurchaseDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PurchaseDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" id="Image" name="Image" class="form-control"  size="300"/>
                @*@Html.EditorFor(model => model.Image, new { htmlAttributes = new { @class = "form-control",@type="file" } })*@
                @Html.ValidationMessageFor(model => model.Image, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input  type="submit" value="创建" class="btn btn-default" />
            </div>
        </div>
    </div>
</form>

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/bootstrap-datepicker")
    <script type="text/javascript">
        $('#PurchaseDate').datepicker({
            format: "yyyy/mm/dd",
            todayBtn: "linked",
            language: "zh-CN",
            autoclose:true,
        });

     



       

        //$("#Image").bind('change', function () {
        //    if(this.files[0].size>300)
        //    $('span[data-valmsg-for="Image"]').text ( '123');
        //    //this.files[0].size gets the size of your file.
        //    //this.files[0].size;
            

        //});


      
        $("#submit").click(function () {
            var formdata = new FormData(this);
            console.log($("input[name='__RequestVerificationToken']").val());
            formdata.append("__RequestVerificationToken", $("input[name='__RequestVerificationToken']").val())
            formdata.append("Name", $('#Name').val())
            formdata.append("PurchaseDate", $('#PurchaseDate').val())
            formdata.append("TypeId", $('#TypeId').val())

            if($('#Image').val()!=""){
                formdata.append("Image",$('#Image')[0].files[0]);
            }
          

            //encodeURIComponent(dataurl);

            console.log($("#Image").val())
            //encodeURIComponent(dataurl);

            $.ajax({
                type: 'POST',
                url: '@Url.Action("Create")',
                data: formdata,
                contentType: false,
                processData: false,
                complete: function (xhr) {
                    console.log(xhr)
           
                        location.href = xhr.getResponseHeader("Location");
                   
                },

            });
        });

      
        //document.getElementById('Image').addEventListener('change', handleFileSelect, false);

    </script>
}
